import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by IntelliJ IDEA.
 * User: caporegim
 * Date: 08.08.12
 * Time: 22:52
 * To change this template use File | Settings | File Templates.
 */
public class LockFreeFibonachiGenerator implements Fibonachi{
    private volatile  AtomicReference ref1 = new AtomicReference(new BigInteger("0"));
    private volatile  AtomicReference ref2 = new AtomicReference(new BigInteger("1"));
    @Override
    public BigInteger next() {
        BigInteger value1 = ( BigInteger) ref1.get();
        BigInteger value2 = ( BigInteger) ref2.get();
        BigInteger value = value1.add(value2);
        ref1.compareAndSet(value1, value2);
        ref2.compareAndSet(value2, value);
        return value;
    }

}
